/**
 * @fileOverview 登录
 * @author bian17888
 */

(function() {

  'use strict';

  /**
   * 登录
   * @module login
   * @see module:common/utils
   */
  define(['utils'], function() {

    $(function() {

      init();

      /**
       * @func init
       * @desc 本页面初始化
       */
      function init() {
        bindEvent();
      }

      /**
       * @func bindEvent
       * @desc 本页面事件绑定
       */
      function bindEvent() {
    	  var cellphone;
    	  
    	  $('.home-wrap')
    	  .on('click', '.r-btn-close', xxFn);

    	  $('.form-register nav a').click(function(){
    		  var id = $(this).attr('id');
    		  $(this).closest('nav').find('a').removeClass('active');
    		  $(this).addClass('active');
    		  if (id == 'personal') {
    			  $('#form_personal').show();
    			  $('#form_enterprise').hide();
    		  } else {
    			  $('#form_personal').hide();
    			  $('#form_enterprise').show();
    		  }
    	  })

    	  /**
    	   * 点击“新建企业账户”
    	   */
    	  $('.form-register #create_enterprise').click(function(){
    		  $(".form-register nav").closest('nav').find('a').removeClass('active');
    		  $('#form_personal').hide();
    		  $('#form_enterprise').show();
    		  $('#enterprise').addClass('active');
    	  })

    	  /**
    	   * 企业注册 其他 选中显示编辑框
    	   */
    	  $('#ckother').click(function(){
    		  if($('#ckother').is(':checked')==true){
    			  $('#otherlabel').css("display","block");
    		  }else{
    			  $('#otherlabel').css("display","none");
    		  }
    		  
//    		  alert($('#ckother').parent().nextAll().html());
    	  })
    	  
    	  
    	  function getCode(){    
    		  var num = Math.random()*1000 + 8999;
    		  return num = parseInt(num, 10);
    	  }

    	  
    	  /**
    	   * 个人用户注册 点击“获取验证码” 按钮
    	   */
    	  $('#form_personal #getcode').click(function(){
    		  var tel = $('#mobile').val();
    		  var reg =  /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
    		  if (!reg.test(tel)) {
    			  alert("请正确填写您的手机号码");
    			  return;
    		  }else{
    			  //这里可以执行一揽子操作
    			  debugger;
    			  var code = getCode();
    			  var regCellPhone = $("#mobile").val();
    			  $.ajax({
         		        type:"GET",
         		        contentType: 'application/json', 
         		        url:"/getValidCode/"+ regCellPhone +"/" + code,        
         		        success: function(response){	
         		        	if(response == 1){
         		        		regCellPhone =  code
         		        	} else {
         		        		regCellPhone = '';
         		        	}
         		        }
         	    	});
    		  };

    		  var btn = $(this);
    		  //过滤样式
    		  if(btn.hasClass("btn-cyan-simple-wait")){
    			  return;
    		  }
    		  var count = 60;
    		  btn.removeClass("btn-cyan-simple").addClass("btn-cyan-simple-wait");//移除默认样式
    		  var resend = setInterval(function(){
    			  count--;
    			  if (count > 0){
    				  btn.html("重新发送 <em>("+count+"s)</em>");
    				  //这里执行一揽子操作
//  				  $.cookie("captcha", count, {path: '/', expires: (1/86400)*count});
    			  }else {
    				  clearInterval(resend);
    				  btn.removeClass("btn-cyan-simple-wait").addClass("btn-cyan-simple");//移除默认样式
    				  btn.html("获取验证码");
    			  }
    		  }, 1000);
    	  })
    	  
      	  /**
    	   * 重置密码 点击“获取验证码” 按钮
    	   */
    	  $('#getuserform #getcode').click(function(){
    		  var tel = $('#mobile').val();
    		  var reg =  /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
    		  if (!reg.test(tel)) {
    			  alert("请正确填写您的手机号码");
    			  return;
    		  }else{
    			  //这里可以执行一揽子操作/getAdminValidCode/{companyid}/{code}    			  

    			  //这里可以执行一揽子操作
    			  debugger;
    			  var code = getCode();
    			  var regCellPhone = $("#mobile").val();
    			  var cmpanyid =  $("#companyId").val();
    			  $.ajax({
         		        type:"GET",
         		        contentType: 'application/json', 
         		        url:"/getAdminValidCode/" + cmpanyid + "/" + regCellPhone +"/" + code,        
         		        success: function(response){	
         		        	if(response == 1){
         		        		regCellPhone =  code
         		        	} else {
         		        		regCellPhone = '';
         		        	}
         		        }
         	    	});
    		  };

    		  var btn = $(this);
    		  //过滤样式
    		  if(btn.hasClass("btn-cyan-simple-wait")){
    			  return;
    		  }
    		  var count = 60;
    		  btn.removeClass("btn-cyan-simple").addClass("btn-cyan-simple-wait");//移除默认样式
    		  var resend = setInterval(function(){
    			  count--;
    			  if (count > 0){
    				  btn.html("重新发送 <em>("+count+"s)</em>");
    				  //这里执行一揽子操作
//  				  $.cookie("captcha", count, {path: '/', expires: (1/86400)*count});
    			  }else {
    				  clearInterval(resend);
    				  btn.removeClass("btn-cyan-simple-wait").addClass("btn-cyan-simple");//移除默认样式
    				  btn.html("获取验证码");
    			  }
    		  }, 1000);
    	  })



      }
      
      
      /**
       * 企业用户注册表单验证
       */
      var vads=$("#form_enterprise").validate({
    	  errorElement: "em",
    	  errorClass:"usererror",
    	  rules: {
    		  companyName: {
    			  required: true
    		  },
    		  address: {
    			  required: true
    		  },
    		  registeredphone: {
    			  required: true
    		  },
    		  bankno:{
    			  required: true
    		  },
    		  bankname:{
    			  required: true,
    		  },
    		  businessid:{
    			  required: true,
    		  },
    		  organizationcode:{
    			  required: true,
    		  },
    		  taxno:{
    			  required: true,
    		  }
    	  },messages: {

    	  }
      });
      
      /**
       * 个人用户注册表单验证
       */
      var vad= $("#form_personal").validate({
    	  errorElement: "em",
    	  errorClass:"usererror",
    	  rules: {
    		  username: {
    			  required: true,
    			  rangelength:[5,11]
    		  },
    		  password: {
    			  required: true,
    			  isPassword:true,
    		  },
    		  password2: {
    			  required: true,
    			  equalTo: "#password"
    		  },
    		  mobile:{
    			  required: true,
    			  isMobile:true,

    		  },
    		  mobilecode:{
    			  required: true,
    			  minlength:4,
    			  maxlength:4
    		  },
    		  companycode:{
    			  required: true,
    			  minlength:4,
    			  maxlength:4
    		  }
    	  },messages: {

    	  }
      });

      
      /**
       * 用户登录表单验证
       */
      var vad= $("#loginform").validate({
    	  errorElement: "em",
    	  errorClass:"usererror",
    	  rules: {
    		  username: {
    			  required: true,
    			  rangelength:[5,11]
    		  },
    		  password: {
    			  required: true,
    			  rangelength:[5,20]
    		  },
    		  mobilecode:{
    			  required: true,
    			  minlength:4,
    			  maxlength:4
    		  }
    	  },messages: {

    	  }
      });
 
      
      /**
       * 重置密码表单验证
       */
      var vad= $("#getuserform").validate({
    	  errorElement: "em",
    	  errorClass:"usererror",
    	  rules: {
    		  username: {
    			  required: true,
    			  rangelength:[5,11]
    		  },
      		  password: {
    			  required: true,
    			  isPassword:true,
    		  },
    		  password2: {
    			  required: true,
    			  equalTo: "#password"
    		  },
    		  mobile:{
    			  required: true,
    			  isMobile:true,

    		  },
    		  mobilecode:{
    			  required: true,
    			  minlength:4,
    			  maxlength:4
    		  }
    	  },messages: {

    	  }
      });
      
      
      
      /**
       * @func xxFn
       * @desc xxFn 描述
       * @param {Object} params - 参数描述
       */
      function xxFn(params) {
        // to do some thing
      }

    });

  });

})();

